import { createApp } from 'vue';
import App from './App.vue';
import LoginApp from './LoginApp.vue';
import router from './router';
import store from './store';
import ShareComponent from '@/components/index';

import 'highlight.js/styles/atom-one-dark.css';
import 'highlight.js/lib/common';
import HljsVuePlugin from '@highlightjs/vue-plugin';
import ElementPlus from '@/components/element-plus';
import FormCreate from '@/components/form-create';

import '@/assets/css/animate.min.css';
import '@/assets/css/index.scss';

import PF, { isTrue, useAuth } from 'pf-component';
// import "pf-component/src/assets/element/index.css";
import 'pf-component/lib/index.css';

import { BaseApi, ServiceEnum } from '@/constants/api/base-api';
import { Constants } from '@/constants/constants';
import { SysUser } from 'pf-component';
import { initByToken } from '@/constants/util/init';

const login = false;
if (login) {
  createApp(LoginApp)
    .use(ElementPlus)
    .use(PF, {
      authUrl: '/pf-auth'
      // authUrl: BaseApi.getUrlNotPrefix(ServiceEnum.auth)
    })
    .mount('#app');
} else {
  const param = {
    clientId: process.env.VUE_APP_CLIENT_ID || '',
    secret: Constants.CLIENT_SECRET || '',
    redirect: Constants.REDIRECT_URL || '',
    systemUrl: BaseApi.getUrl(ServiceEnum.system),
    authUrl: BaseApi.getUrlNotPrefix(ServiceEnum.auth),
    authCodeUrl: BaseApi.getAuthCodeHref(),
    // refreshToken: false,
    httpRequestInit: (requestInit: RequestInit) => {
      (requestInit.headers as any)['APP_ID'] = store.getters.currentAppId;
      const userInfo: SysUser = store.getters.userInfo;
      if (!userInfo) {
        return;
      }
      const { tenId } = userInfo;
      (requestInit.headers as any)['USER_TEN_ID'] = tenId || '';
    }
  };
  const app = createApp(App);
  app
    .use(store)
    .use(ElementPlus)
    .use(FormCreate)
    .use(ShareComponent)
    .use(HljsVuePlugin)
    .use(PF, param);
  const hrefUrl = new URL(location.href);
  const token = hrefUrl.searchParams.get('access_token');
  // ||
  // 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJzY29wZSI6WyJhbGwiXSwibmFtZSI6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vfuXpHSDV3EISTbi0jXLkrP-HPXRpt1Ss';
  const jti = hrefUrl.searchParams.get('jti');
  // || '6628bebe-4960-45f7-839e-eef83fbdfa4b';
  if (token && jti) {
    useAuth().setToken({
      accessToken: token,
      refreshToken: 'xxx',
      jti
    });
    initByToken().then(res => {
      if (res) {
        app.use(router).mount('#app');
      } else {
        console.error('initByToken Error...');
      }
    });
  } else {
    app.use(router).mount('#app');
  }
}
